package com.lemon.smartsafetynew.ui.adapter.work;

import android.graphics.Color;
import android.view.Gravity;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.github.mikephil.charting.animation.Easing;
import com.github.mikephil.charting.charts.RadarChart;
import com.github.mikephil.charting.components.AxisBase;
import com.github.mikephil.charting.components.Legend;
import com.github.mikephil.charting.components.MarkerView;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.RadarData;
import com.github.mikephil.charting.data.RadarDataSet;
import com.github.mikephil.charting.data.RadarEntry;
import com.github.mikephil.charting.formatter.IAxisValueFormatter;
import com.github.mikephil.charting.formatter.IValueFormatter;
import com.github.mikephil.charting.interfaces.datasets.IRadarDataSet;
import com.github.mikephil.charting.utils.ViewPortHandler;
import com.lemon.smartsafety.R;
import com.lemon.smartsafety.app.Constants;
import com.lemon.smartsafety.entity.main.MainRcvItemBase;
import com.lemon.smartsafety.utils.app.UIHelper;
import com.lemon.smartsafetynew.entity.User;
import com.lemon.smartsafetynew.entity.ledger.LedgerRadarVo;
import com.lemon.smartsafetynew.ui.activity.ledger.LedgerHomeActivity;
import com.lemon.smartsafetynew.ui.widget.marker.RadarMarkerView;
import com.lemon.smartsafetynew.util.common.CommonUtil;
import com.lemon.smartsafetynew.util.user.AccountUtil;
import com.thefinestartist.finestwebview.FinestWebView;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import kale.adapter.item.AdapterItem;

/**
 * Created by Andrew on 2017/8/16.
 */

public class WorkChartViewRec implements AdapterItem<MainRcvItemBase<LedgerRadarVo>> {


    private FrameLayout frameLayout;
    private ImageView helpImageView;
    private TextView companyStatusTv;
    private TextView companyNameTv;
    private RadarChart radarChart;

    @Override
    public int getLayoutResId() {
        return R.layout.view_dashboard;
    }

    @Override
    public void bindViews(View root) {
        frameLayout = (FrameLayout) root.findViewById(R.id.frameLayout);
        helpImageView = (ImageView) root.findViewById(R.id.help_imageView);
        companyStatusTv = (TextView) root.findViewById(R.id.company_status_tv);
        companyNameTv = (TextView) root.findViewById(R.id.company_name_tv);
        radarChart = (RadarChart) root.findViewById(R.id.radar_chart);

    }

    @Override
    public void setViews() {

        helpImageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onHelpClick(v);
            }
        });

        frameLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                UIHelper.showActivity(v.getContext(), LedgerHomeActivity.class);
            }
        });

    }

    @Override
    public void handleData(MainRcvItemBase<LedgerRadarVo> ledgerRadarVoMainRcvItemBase, int position) {

        LedgerRadarVo data = ledgerRadarVoMainRcvItemBase.getData();
        companyStatusTv.setText(data.getStatus());

        User user = AccountUtil.getUser();
        if(user != null) {
            companyNameTv.setText(CommonUtil.avoidNullString(user.getCompanyName()));
        }

        List<Float> list = new ArrayList<>();
        list.add(data.getSecurity_patrol());
        list.add(data.getHidden_danger());
        list.add(data.getDynamic_monitoring());
        list.add(data.getDevice_management());
        list.add(data.getBuilding_safety());

        initRadarChart(list);

    }

    private void onHelpClick(View view) {
        new FinestWebView.Builder(view.getContext()).theme(R.style.FinestWebViewTheme)
                .showUrl(false)
                .statusBarColorRes(R.color.colorPrimaryDark)
                .toolbarColorRes(R.color.colorPrimary)
                .titleColorRes(R.color.white)
                .iconDefaultColorRes(R.color.finestWhite)
                .progressBarColorRes(R.color.finestWhite)
                .showSwipeRefreshLayout(true)
                .swipeRefreshColorRes(R.color.colorPrimaryDark)
                .menuSelector(R.drawable.selector_light_theme)
                .menuTextGravity(Gravity.CENTER)
                .menuTextPaddingRightRes(R.dimen.defaultMenuTextPaddingLeft)
                .dividerHeight(0)
                .gradientDivider(false)
                .setCustomAnimations(R.anim.slide_left_in, R.anim.hold, R.anim.hold, R.anim.slide_right_out)
                .showMenuShareVia(false)
                .stringResCopiedToClipboard(R.string.web_view_copied)
                .stringResCopyLink(R.string.web_view_copy)
                .stringResOpenWith(R.string.web_view_open)
                .show(Constants.SCORE_HELP_URL); //TODO change the help URL
    }

    private void initRadarChart(List<Float> values) {

        //radarChart.setBackgroundColor(Color.rgb(0x0a, 0xBB, 0xA0));
        radarChart.setBackgroundColor(0x00000000);

        radarChart.getDescription().setEnabled(false);

        radarChart.setNoDataText("无数据");
        radarChart.setWebLineWidth(1f);
        radarChart.setWebColor(Color.WHITE);
        radarChart.setWebLineWidthInner(1f);
        radarChart.setWebColorInner(Color.WHITE);
        radarChart.setWebAlpha(100);
        radarChart.setRotationEnabled(false);
        radarChart.setTouchEnabled(false);

        // create a custom MarkerView (extend MarkerView) and specify the layout
        // to use for it
        MarkerView mv = new RadarMarkerView(radarChart.getContext(), R.layout.radar_markerview);
        mv.setChartView(radarChart); // For bounds control
        radarChart.setMarker(mv); // Set the marker to the chart

        setData(values);

        radarChart.animateXY(
                1400, 1400,
                Easing.EasingOption.EaseInOutQuad,
                Easing.EasingOption.EaseInOutQuad);

        XAxis xAxis = radarChart.getXAxis();
        xAxis.setTextSize(9f);
        xAxis.setYOffset(0f);
        xAxis.setXOffset(0f);
        xAxis.setValueFormatter(new IAxisValueFormatter() {

            private String[] mActivities = new String[]{"安全巡查", "安全隐患", "动态监测", "设备管理", "建筑安全"};

            @Override
            public String getFormattedValue(float value, AxisBase axis) {
                return mActivities[(int) value % mActivities.length];
            }
        });

        xAxis.setTextColor(Color.WHITE);

        YAxis yAxis = radarChart.getYAxis();
        yAxis.setLabelCount(5, false);
        yAxis.setTextSize(9f);
        yAxis.setAxisMinimum(0f);
        yAxis.setAxisMaximum(9f);
        yAxis.setDrawLabels(false);

        Legend l = radarChart.getLegend();
        l.setEnabled(false);
//        l.setVerticalAlignment(Legend.LegendVerticalAlignment.BOTTOM);
//        l.setHorizontalAlignment(Legend.LegendHorizontalAlignment.CENTER);
//        l.setOrientation(Legend.LegendOrientation.HORIZONTAL);
//        l.setDrawInside(false);
//        l.setXEntrySpace(0f);
//        l.setYEntrySpace(10f);
//        l.setTextColor(Color.WHITE);
    }

    public void setData(List<Float> values) {

        if (values == null) {
            return;
        }

        ArrayList<RadarEntry> entries1 = new ArrayList<>();

        for (Float i : values) {
            entries1.add(new RadarEntry(i));
        }

        RadarDataSet set1 = new RadarDataSet(entries1, "本周");
        set1.setColor(Color.rgb(0x38, 0xb9, 0xdf));
        set1.setFillColor(Color.rgb(0x3b, 0xae, 0xe0));
        set1.setValueFormatter(new IValueFormatter() {
            @Override
            public String getFormattedValue(float value, Entry entry, int dataSetIndex, ViewPortHandler viewPortHandler) {
                return String.valueOf((int) value);
            }
        });
        set1.setDrawFilled(true);
        set1.setFillAlpha(180);
        set1.setLineWidth(2f);
        set1.setDrawHighlightCircleEnabled(true);
        set1.setDrawHighlightIndicators(false);

        ArrayList<IRadarDataSet> sets = new ArrayList<>();
        sets.add(set1);

        RadarData data = new RadarData(sets);
        data.setValueTextSize(8f);
        data.setDrawValues(false);
        data.setValueTextColor(Color.WHITE);

        radarChart.setData(data);
        radarChart.invalidate();
    }

}
